import jieba, wordcloud

with open("分词.txt", 'r', encoding='utf8') as f:
    content = f.read()

# 分词
lst = jieba.lcut(content)
print(lst)
# 实现去重
set1 = set(lst)
print(set1)

dict1 = dict()
for key in set1:
    if len(key) >= 2:
        value = lst.count(key)
        dict1[key] = value

list2 = []
for k, v in dict1.items():
    # print(f'{k} 出现了 {v}次')
    list2.append([k, v])
#
list2.sort(key=lambda x: x[1], reverse=True)
print(list2[:10])

# 词云图
txt = ' '.join(lst)
print(txt)
stop_word = ["\n", '，', '一个', "了"]
wc = wordcloud.WordCloud(background_color="white", \
                    font_path="msyh.ttc", \
                    stopwords=stop_word, \
                    width=800, \
                    height=600)
wc.generate(txt)
wc.to_file("词云图实例.png")
